package com.neusoft.elmboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.elmboot.Dto.BussinessDto;
import com.neusoft.elmboot.Vo.BussinessVo;
import com.neusoft.elmboot.common.result.Result;
import com.neusoft.elmboot.po.BusinessEntity;
import com.neusoft.elmboot.service.BusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/business")
@Slf4j
@Api(tags = "商家管理")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @GetMapping
    public Result<List<BussinessVo>> getBusinessId() {
        List<BussinessVo> bussinessVoList = businessService.getBussinessVoList();

        return Result.success(bussinessVoList);
    }

    /**
     * 商家分页查询
     * @param page
     * @param pageSize
     * @param businessId
     * @param businessName
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("商家分页查询")
    public Result<Page> page(Integer page,Integer pageSize,String businessId,String businessName){
        log.info("商家分页查询,{},{},{},{}",page,pageSize, businessId, businessName);
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<BusinessEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(businessId!=null,BusinessEntity::getBusinessId, businessId);
        lqw.like(businessName != null, BusinessEntity::getBusinessName, businessName);
        businessService.page(pageInfo, lqw);

        return Result.success(pageInfo);

    }

    /**
     * 商家添加
     * @param bussinessDto
     * @return
     */
    @PostMapping
    @ApiOperation("商家添加")
    public Result save(@RequestBody BussinessDto bussinessDto){
        log.info("商家添加,{}", bussinessDto);
        businessService.add(bussinessDto);
        return Result.success();

    }

    @ApiOperation("根据id查询商家信息")
    @GetMapping("/detail")
    public Result<BussinessVo> getById(Long id){
        log.info("根据id查询商家信息,{}", id);
        BusinessEntity businessEntity = businessService.getById(id);
        BussinessVo bussinessVo = new BussinessVo();
        BeanUtils.copyProperties(businessEntity,bussinessVo);

        return Result.success(bussinessVo);
    }

    /**
     *
     * @param bussinessDto
     * @return
     */
    @ApiOperation("商家更新")
    @PutMapping
    public Result update(@RequestBody BussinessDto bussinessDto){
        log.info("商家更新,{}", bussinessDto);
        BusinessEntity businessEntity = new BusinessEntity();
        BeanUtils.copyProperties(bussinessDto, businessEntity);
        businessService.updateById(businessEntity);
        return Result.success();

    }

    /**
     * 商家删除
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("商家删除")
    public Result remove(Long id){
        log.info("商家删除,{}",id);
        businessService.removeById(id);

        return Result.success();
    }

    @GetMapping("/listBusinessByOrderTypeId")
    public Result<List<BussinessVo>> listBusinessByOrderTypeId(Long orderTypeId){
        LambdaQueryWrapper<BusinessEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BusinessEntity::getOrderTypeId,orderTypeId);
        List<BusinessEntity> list = businessService.list(lqw);
        List<BussinessVo> bussinessVoList = list.stream().map((item) -> {
            BussinessVo bussinessVo = new BussinessVo();
            BeanUtils.copyProperties(item, bussinessVo);
            return bussinessVo;
        }).collect(Collectors.toList());
        return Result.success(bussinessVoList);

    }


    @GetMapping("/getBusinessById")
    public Result<BussinessVo> getBusinessById(Long businessId) {

        BusinessEntity businessEntity = businessService.getById(businessId);
        BussinessVo bussinessVo = new BussinessVo();
        BeanUtils.copyProperties(businessEntity, bussinessVo);
        return Result.success(bussinessVo);
    }

}
